package com.whfc.openapi.api.emp.service;

import com.alibaba.fastjson.JSONObject;
import com.whfc.common.constant.QueueConst;
import com.whfc.common.util.DateUtil;
import com.whfc.common.util.JSONUtil;
import com.whfc.emp.cncit.entity.CncitUpMsgS;
import com.whfc.emp.entity.AppDeviceCardLog;
import com.whfc.emp.service.AppHelmetService;
import com.whfc.hdc.dto.AppDeviceDTO;
import com.whfc.hdc.service.AppDeviceService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.math.BigDecimal;

@Service
public class OpenApiEmpHelmetServiceImpl implements OpenApiEmpHelmetService{
    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private AmqpTemplate amqpTemplate;

    @DubboReference(interfaceClass = AppDeviceService.class, version = "1.0.0")
    private AppDeviceService appDeviceService;

    @DubboReference(interfaceClass = AppHelmetService.class, version = "1.0.0")
    private AppHelmetService appHelmetService;

    @Override
    public void receiveZbznData(String body) {

        JSONObject jsonBody = JSONObject.parseObject(body);

        String type = jsonBody.getString("type");
        if (!StringUtils.hasText(type)){
            logger.error("缺少报文类型");
            return;
        }

        String deviceCode = jsonBody.getString("IMEI");
        // 根据code 查询 设备id
        AppDeviceDTO deviceBySn = appDeviceService.getDeviceBySn(deviceCode);
        // 设备定位数据上传
        if (type.startsWith("hat")) {
            CncitUpMsgS reqMsg = new CncitUpMsgS();
            reqMsg.setDeviceId(deviceBySn.getId());
            // 时间
            String dateString = type.substring(4);
            reqMsg.setTime(DateUtil.parseDateTime(dateString));
            // 经度
            String longitude = jsonBody.getString("longitude");
            reqMsg.setLngWgs84(Double.parseDouble(longitude));
            // 纬度
            String latitude = jsonBody.getString("latitude");
            reqMsg.setLatWgs84(Double.parseDouble(latitude));
            // 电池容量
            BigDecimal batteryLevel = jsonBody.getBigDecimal("batteryLevel");

            reqMsg.setBatteryPower(Integer.parseInt(batteryLevel.setScale( 0, BigDecimal.ROUND_HALF_UP ).toString()));
            Integer posture = jsonBody.getInteger("posture");
            // 脱帽报警
            if (-1 == posture) {
                reqMsg.setAlarmDoff(1);
            }
            // 跌落报警
            if (-2 == posture) {
                reqMsg.setAlarmDrop(1);
            }
            amqpTemplate.convertAndSend(QueueConst.EMP_HELMET_DATA, JSONUtil.toString(reqMsg));
        }

        // 设备sos报警数据上传
        if (type.startsWith("SOS")) {
            Integer flag = jsonBody.getInteger("flag");
            if (flag==2) {
                AppDeviceCardLog log = new AppDeviceCardLog();
                log.setDeviceId(deviceBySn.getId());
                // 经度
                String longitude = jsonBody.getString("longitude");
                log.setLngWgs84(Double.parseDouble(longitude));
                // 纬度
                String latitude = jsonBody.getString("latitude");
                log.setLatWgs84(Double.parseDouble(latitude));
                // 时间
                String dateString = type.substring(4);
                log.setTime(DateUtil.parseDateTime(dateString));
                log.setAlarmSos(1);
                appHelmetService.checkWarn(log);
            }
        }
    }
}
